package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.api.cache.prefix.UserInfoKeyPrefix;
import cn.wolfcode.luowowo.api.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(Long id){
        System.out.println(userInfoService.get(id));
        return "OK";
    }

    /**
     * 检查手机号码是否存在
     * @param
     * @return
     */


    @RequestMapping("/checkPhone")
    @ResponseBody
    public boolean checkPhone(String phone){

        return !userInfoService.checkPhone(phone);
    }
    @RequestMapping("/userRegist")
    @ResponseBody
    public AjaxResult userRegist(String phone, String nickname, String password, String rpassword, String verifyCode){
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
        return new AjaxResult();
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public AjaxResult userLogin(HttpServletResponse response, HttpSession session,String username, String password){
        String token = userInfoService.userLogin(username,password);
        //添加cookie到浏览器中
        CookieUtil.addCookie(response, UserInfoKeyPrefix.TOKEN_COOKIE_NAME,token);
        //登录成功之后的信息存入了redis中,所以跳转到主页面中是没有信息的,要去redis中取
        session.setAttribute("userInfo",userInfoRedisService.getUserInfoByToken(UserInfoKeyPrefix.USER_INFO_TOKEN,token));
        return new AjaxResult();
    }
}
